package gui.messageview;


import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import query.MessageQuery;
import no.ntnu.fp.model.CurrentUser;
import no.ntnu.fp.model.Message;

public class MyMessagesWindow extends JPanel{
	
	private static final long serialVersionUID = 1L;
	private DefaultListModel model;
	private JList MessageList;
	private ThisMessageWindow SelectedMelding;

	
	public MyMessagesWindow(){
		model = new DefaultListModel();
		ArrayList<Message> meldinger = MessageQuery.getMyMessages(CurrentUser.getCurrentUser());
		while(!meldinger.isEmpty()){
			model.addElement(meldinger.remove(0));
		}/*
		Person lol = new Person("megselv :O", "Epost@post.no", new Date());
		Message hei = new Message(lol, lol,"Hei dette er en melding", "Dette er teksten i meldingen", -1, -1 ,MessageType.SUMMON);
		for(int i = 0; i<40;i++){model.addElement(hei);}*/
		MessageList = new JList(model);
		MessageList.addListSelectionListener(new ListSelect());
		JScrollPane scroll = new JScrollPane(MessageList);
		MessageList.setCellRenderer(new MessageListRenderer());
		JLabel top = new JLabel("Mine Meldinger:");
		top.setFont(new Font("Arial", Font.PLAIN, 20));
		GridBagConstraints leyout = new GridBagConstraints();
		JFrame ramme = new JFrame();
		ramme.setSize(100, 80);
		GridBagLayout gt = new GridBagLayout();
		gt.rowHeights = new int []{00,400};
		ramme.setLayout(gt);
		leyout.anchor = GridBagConstraints.NORTHWEST;
		ramme.add(top, leyout);
		ramme.setResizable(false);
		leyout.gridy = 1;
		leyout.fill = GridBagConstraints.VERTICAL;	
		ramme.add(scroll, leyout);
		leyout.gridx = 1;
		SelectedMelding = new ThisMessageWindow(this);
		leyout.ipadx = 20;
		ramme.add(SelectedMelding, leyout);
		ramme.setVisible(true);
		ramme.pack();
		
		
	}
	
	
	public void deSelect() {
		MessageList.clearSelection();
	}


	

	public class ListSelect implements ListSelectionListener{

		
		public void valueChanged(ListSelectionEvent e) {
			if(!MessageList.isSelectionEmpty()){
				int i = MessageList.getSelectedIndex();
				SelectedMelding.setModel((Message) model.get(i));
				}
			else{
				SelectedMelding.setDefault();
				
			}
			
		}
		
	}
	
	
}
